<?php

namespace common\models;

use common\base\BModel;
class LianWebModel extends BModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'lian_web';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id','create_time','type','status','recommend','click','auth_time'], 'integer'],
            [['title','url','image','desc'], 'string', 'max' => 100],
            [['title','url','image'], 'on' => ['add'], 'message' => '{attribute}不能为空'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => '名称',
            'url' => 'url',
            'image'=> '封面',
            'type'=>'类型',
            'desc'=> '简介',
            'click'=>'点击次数',
            'status'=> '状态',
            'recommend'=> '是否推荐',
            'auth_time'=>'审核时间',
            'create_time'=>'创建时间'
        ];
    }

    /**
     * 保存前
     * @param bool $insert
     * @return bool
     */
    public function beforeSave($insert)
    {
        if(parent::beforeSave($insert)){
            $time = time();
            if($this->isNewRecord){
                $this->create_time = $time;
            }
            return true;
        }
        return false;
    }

    public function getTypeName()
    {
        return $this->hasOne(LianTypeModel::className(), ['id' => 'type']);
    }

    public function getTypeTitle()
    {
        $type = LianTypeModel::find()->select(['name'])->where(['id'=>$this->type])->asArray()->one();
        return !empty($type) ? $type['name'] : '';
    }

    public function getAuthTime(){
        $str = '未通过审核';
        if(!empty($this->auth_time)){
            $str = date('Y-m-d H:i:s',$this->create_time);
        }
        return $str;
    }

    public function getRecommendStatus()
    {
        $str = '未推荐';
        if($this->recommend == 1){
            $str = '已推荐';
        }
        return $str;
    }

}
